package com.credit.jumu.calculator.activity;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;

import com.credit.jumu.calculator.R;
import com.credit.jumu.calculator.utils.LogUtils;
import com.credit.jumu.calculator.utils.PermissionUtils;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;

@EActivity(R.layout.activity_loding)
public class LoadingActivity extends AppCompatActivity {

	private boolean isOk = false;
	private final String[] PERMISSIONS = new String[]{Manifest.permission.READ_PHONE_STATE,
			Manifest.permission.READ_EXTERNAL_STORAGE};
	private final int REQUEST_CODE_PERMISSIONS = 2;

	@AfterViews
	public void initView() {
		//setContentView(R.layout.activity_loding);
		handler.sendEmptyMessageDelayed(0, 2000);
	}

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		//goMain();
		//this.finish();

	}

	@Override
	protected void onResume() {
		super.onResume();
		PermissionUtils.checkMorePermissions(this, PERMISSIONS, new PermissionUtils.PermissionCheckCallBack() {
			@Override
			public void onHasPermission() {
				if (isOk) {
					goMain();
				} else {
					isOk = true;
				}
			}

			@Override
			public void onUserHasAlreadyTurnedDown(String... permission) {
				showExplainDialog(permission, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						LogUtils.println("申请权限222");
						PermissionUtils.requestMorePermissions(LoadingActivity.this, PERMISSIONS, REQUEST_CODE_PERMISSIONS);
					}
				});
			}

			@Override
			public void onUserHasAlreadyTurnedDownAndDontAsk(String... permission) {
				LogUtils.println("申请权限333");
				PermissionUtils.requestMorePermissions(LoadingActivity.this, PERMISSIONS, REQUEST_CODE_PERMISSIONS);
			}
		});
	}

	/**
	 * 解释权限的dialog
	 */
	private void showExplainDialog(String[] permission, DialogInterface.OnClickListener onClickListener) {
		new AlertDialog.Builder(this)
				.setTitle("申请权限")
				.setMessage("我们需要" + "通话权限和访问文件" + "权限")
				.setPositiveButton("确定", onClickListener)
				.show();
	}

	public void goMain() {
		Intent intent = new Intent(LoadingActivity.this, MainActivity_.class);
		startActivity(intent);
		this.finish();
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			LogUtils.println("获取权限333" + isOk);
			if (isOk) {
				goMain();
			} else {
				isOk = true;
			}
			super.handleMessage(msg);
		}
	};

	@Override
	public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

		PermissionUtils.onRequestMorePermissionsResult(LoadingActivity.this, PERMISSIONS, new PermissionUtils.PermissionCheckCallBack() {
			@Override
			public void onHasPermission() {
				if (isOk) {
					goMain();
				} else {
					isOk = true;
				}
			}

			@Override
			public void onUserHasAlreadyTurnedDown(String... permission) {
				LogUtils.println("申请权限555");
			}

			@Override
			public void onUserHasAlreadyTurnedDownAndDontAsk(String... permission) {
				LogUtils.println("申请权限999");
			}
		});
		LogUtils.println("获取权限666" + isOk + requestCode + "---" + permissions.toString() + "---" + grantResults.toString());
	}

	/**
	 * 显示前往应用设置Dialog
	 */
	private void showToAppSettingDialog() {
		new AlertDialog.Builder(LoadingActivity.this)
				.setTitle("需要权限")
				.setMessage("我们需要相关权限，才能实现功能，点击前往，将转到应用的设置界面，请开启应用的相关权限。")
				.setPositiveButton("前往", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						PermissionUtils.toAppSetting(LoadingActivity.this);
					}
				})
				.setNegativeButton("取消", null).show();
	}

}
